﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/SystemManagement/Views/Shared/LigerUI.Master"
    Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    UserRole
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">

    var UserId=<%=ViewData["UserId"]%>;

    jQuery(function () {

            $("#toptoolbar").ligerToolBar({ items: [
                { text: '保存', click: f_save, icon: 'save' },
                { line: true },
                { text: '添加', click: f_add, icon: 'add' },
                { line: true },
                { text: '删除', click: f_delete, icon: 'delete' },
                { line: true },
                { text: '关闭', click: f_close, icon: 'close' }
                ]
                });


            grid = $("#maingrid").ligerGrid({
                columns: [{ display: '主键', name: 'RoleId', isAllowHide: false, hide: 1, width: 40 },
                    { display: '角色名', name: 'RoleName' },
                    { display: '描述', name: 'Description' }],
                url: encodeURI('<%=Url.Action("GetUserRoles","User")%>'+ '/?UserId='+ <%=ViewData["UserId"]%>),
                sortName: 'RoleId',
                //data: { Rows: []},
                dataAction: 'server',
                usePager: false,
                checkbox: false,
                rownumbers: true,
                width: '100%',
                height: '100%',
                InWindow:true
            });
    })


    function f_save()
    {
            $.ajax({
            type: "POST",
            url: '<%=Url.Action("SaveUserRoles","User") %>',
            data: "Roles="+JSON.stringify(grid.getData())+"&UserId="+UserId,
            success: function(msg){
                $.ligerDialog.alert("保存成功！");
            }
        });

    }


    var dia1;
    function f_add()
    {
    
        dia1 = $.ligerDialog.open({ title: '选择角色', width: 400, height: 300, url:  encodeURI('<%=Url.Action("UserRoleSelect","User")%>'), buttons: [
                { text: '确定', onclick: f_importOK },
                { text: '取消', onclick: f_importCancel }
            ]
            }); 

    }

    function f_importOK(item, dialog)
    { 

        var rows = dialog.frame.f_select();
        if (!rows)
        {
            alert('请选择行!');
            return;
        } 
        for (var i = 0; i < rows.length; i++)
        {
            var hasRoleId=false;
            var gridRows=grid.getData();

            for(var j=0;j<gridRows.length;j++)
            {
                //alert(gridRows[j].RoleId+","+rows[i].RoleId);
                if(gridRows[j].RoleId==rows[i].RoleId)
                {
                    hasRoleId=true;
                    break;
                }
            }

            if(!hasRoleId)
            {
                grid.addRow(rows[i]);
            }

        } 
        dialog.close();
    }
    function f_importCancel(item, dialog)
    { 
        dialog.close();
    }


    function f_delete()
    {
        //var manager = $("#maingrid").ligerGetGridManager();
        grid.deleteSelectedRow();

    }
    function f_close()
    {
        parent.w.close();
    }
    </script>
    <style type="text/css">
        body
        {
            overflow: hidden;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <div id="toptoolbar">
    </div>
    <div id="maingrid" style="margin: 0; padding: 0">
    </div>
</asp:Content>
